/**
 * 1658. 将 x 减到 0 的最小操作数
 */
public class No1658 {
    public int minOperations(int[] nums, int x) {
        int left = 0, right = 0;

        int total = -x;
        for (int num : nums) {
            total += num;
        }
        if (total == 0) {
            return nums.length;
        }

        int sum = 0;
        int result = -1;
        int len = 0;

        for (; right < nums.length; right++) {
            sum += nums[right];
            for (; left < nums.length && sum > total; left++) {
                sum -= nums[left];
            }
            if (sum == total) {
                int temp = right - left + 1;
                if (temp > len) {
                    len = temp;
                    result = nums.length - len;
                }
            }
        }
        return result;
    }
}
